$(function () {
    var swiperWidth = $(".swiper-container").width();
    var index = 0;
    var timer = null;
    autoPlay();
    // 点击切换
    $(".swiper-list li").click(function () {
        $(this).addClass("active").siblings().removeClass("active");
        index = $(this).index();
        $(".swiper-bar").animate({ left: -swiperWidth * index })
    })
    $(".swiper-container").hover(function () {
        clearInterval(timer);
        $(".rightSlide").css("display", "block")
        $(".leftSlide").css("display", "block")
    }, function () {
        autoPlay();
        $(".rightSlide").css("display", "none")
        $(".leftSlide").css("display", "none")
    })
    $(".rightSlide").click(function () {
        index++;
        var activeIndex = index >= $(".swiper-list li").length ? 0 : index;
        $(".swiper-list li").eq(activeIndex).addClass("active").siblings().removeClass("active");
        $(".swiper-bar").animate({ left: -swiperWidth * index }, function () {
            if (index >= $(".swiper-list li").length) {
                $(this).css({ left: 0 });
                index = 0;
            }
        })
    })
    $(".leftSlide").click(function () {
        index--;
        if (index < 0) {
            index = 3
        }
        var activeIndex = index >= $(".swiper-list li").length ? 0 : index;
        $(".swiper-list li").eq(activeIndex).addClass("active").siblings().removeClass("active");
        $(".swiper-bar").animate({ left: -swiperWidth * index }, function () {
            if (index >= $(".swiper-list li").length) {
                $(this).css({ left: 0 });
                index = 0;
            }
        })
    })

    function autoPlay() {
        clearInterval(timer);
        timer = setInterval(function () {
            index++;
            var activeIndex = index >= $(".swiper-list li").length ? 0 : index;
            $(".swiper-list li").eq(activeIndex).addClass("active").siblings().removeClass("active");
            $(".swiper-bar").animate({ left: -swiperWidth * index }, function () {
                if (index >= $(".swiper-list li").length) {
                    $(this).css({ left: 0 });
                    index = 0;
                }
            })
        }, 2500);
    }
})


var page = 1;
var maxPage = 1;
var total = 1;
dtxr(page);
$('.next').click(function () {
    if (page >= maxPage) {
        page--;
    }
    page++;
    console.log(page);
    dtxr(page);
    $(".searchCon").val("")
});
$('.prev').click(function () {
    if (page <= 1) page++;
    page--;
    console.log(page);
    dtxr(page);
    $(".searchCon").val("")
});

var zts = 0
function dtxr(page, title = "", type = "asc") {
    $.ajax({
        url: '../php/searchAllId.php',
        data: { page: page, size: 16, title, type },
        type: 'get',
        dataType: 'json',
        success: function (data) {
            maxPage = data.maxPage
            zts = data.total
            var { list } = data;
            // console.log(data);
            var html = '';
            list.forEach(({ title, img, oldmoney, money, id }) => {
                html += `
                <div>
                    <img src="${img}" alt="">
                    <p>
                        <span>￥${money}.00</span>
                        <s> 原价:￥${oldmoney}</s>
                    </p>
                    <p>${title}</p>
                    <p><a href="../html/watch.html?${id}">查看详情</a></p>
                </div>`
            });
            $(".centent").html(html);
            if (page >= maxPage) {
                page = maxPage
            } else if (page <= 1) {
                page = 1
            }
            $('.cur').text(`第${page}页`)
            $(".tips").text(`共${zts}条`)
        }
    }
    );
}
$(document).on("click", ".searchBtn", function () {
    dtxr(page, $(".searchCon").val());
})

$("#sheng").click(function () {
    dtxr(page, $(".searchCon").val(), type = "asc")
})
$("#jiang").click(function () {
    dtxr(page, $(".searchCon").val(), type = "desc")
})

$(".resetGrade").click(function () {
    dtxr(1, title = "", type = "desc")
})